#include "StdAfx.h"
#include "ReadConfig.h"

CReadConfig::CReadConfig(ITaskEvent* pTaskEvent):m_pConfigManage(NULL),m_nNodeCount(0),m_nCurrentRunTaskID(-1),
m_pTaskEvent(pTaskEvent)
{
}

CReadConfig::~CReadConfig(void)
{
	Stop();
	if(m_pConfigManage)
		delete m_pConfigManage;
}

BOOL CReadConfig::Create(LPCTSTR lpConfigPath)
{
	m_pConfigManage = new CConfigManage;
	_ASSERT(m_pConfigManage);
	if(m_pConfigManage == NULL)
		return FALSE;
	BOOL bRes = m_pConfigManage->Load(lpConfigPath);
	if(bRes == TRUE)
		m_pConfigManage->AddNodeToTaskList();
	return bRes;
}

UINT CReadConfig::GetNodeCount()
{
	 m_nNodeCount = m_pConfigManage->GetTaskCount();
	 return m_nNodeCount;
}

BOOL CReadConfig::GetNodeInfo(int nIndex,CMaterialInfo& info)
{
	if(m_pConfigManage)
	{
		if(nIndex < 0 || nIndex > m_pConfigManage->GetTaskCount())
			return FALSE;
		if(m_pConfigManage->GetTaskInfo(nIndex,info) == TRUE)
			return TRUE;
	}
	
	return FALSE;
}

void CReadConfig::PostCount(LPCTSTR pstrUrl)
{
	if(m_pConfigManage)
		m_pConfigManage->PostCount(pstrUrl);
}

BOOL CReadConfig::Run(TASKTYPE TaskType)
{
	int nTotalCount = GetNodeCount();
	if(m_pConfigManage == NULL || m_nNodeCount == 0)
		return FALSE;
	BOOL bRes = FALSE;
	m_nCurrentRunTaskID++;
	if(m_nCurrentRunTaskID < nTotalCount)
	{
		bRes = m_pConfigManage->RunTask(TaskType,m_nCurrentRunTaskID,m_pTaskEvent);
	}
	return bRes;

}
void CReadConfig::Stop()
{
	if(m_pConfigManage)
	{
		m_pConfigManage->Pause(GetCurrentTaskID());
		if(m_nCurrentRunTaskID >= 0 && m_nCurrentRunTaskID < GetNodeCount())
			m_nCurrentRunTaskID--;
	}
}

UINT CReadConfig::GetCurrentTaskID()
{
	return m_nCurrentRunTaskID;
}